package com.eryos.android.holidaybudget.db;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.eryos.android.holidaybudget.beans.Budget;
import com.eryos.android.holidaybudget.beans.Expense;


public class BudgetDatabase extends Database {

	public static final int NUMBER_OF_HISTORY = 6;	
	public static final int ROUNDING = 2;
	
	private static final String DATE_FORMAT = "%d/%m/%Y"; 
	private static final String WEEK_FORMAT = "%W"; 
	private static final String MONTH_FORMAT = "%m"; 

	public static String getNow(){
		return "datetime("+System.currentTimeMillis()/1000+", 'unixepoch', 'localtime')";
	}
	
	private String LOG_TAG = this.getClass().getSimpleName();
	
	private static BudgetDatabase currentDB;
	
	/**
	 * Create a new CigaretteDatabase Object link to the context in argument
	 * @param ctx Application context
	 */
	private BudgetDatabase(Context ctx) {
		super(ctx);
		LOG_TAG = getClass().getSimpleName();
	}
	
	public static BudgetDatabase getInstance(Context ctx){
		if ( currentDB == null ){
			currentDB = new BudgetDatabase(ctx);
		}
		
		return currentDB;
	}

	/*****************************************************************************/

	
	public void insertBudget(Budget budget){
    	Log.d(LOG_TAG, "InsertDB Budget : "+budget);
		db.execSQL("INSERT INTO "+BUDGET_TABLE+" VALUES ( null, '"+budget.getName()+"', '"+budget.getType()+"', '"+budget.getInitialAmount()+"');");

    }
	
	public Budget getBudget(String budgetName){
    	Log.d(LOG_TAG, "getBudget : "+budgetName);
		String sqlQuery = "SELECT "+COL_BUDGET_CCY+", "+COL_BUDGET_AMOUNT+" FROM "+BUDGET_TABLE+" WHERE "+COL_BUDGET_NAME+" = '"+budgetName+"';";        

		Cursor mCursor = db.rawQuery(sqlQuery, null);

		Budget res = new Budget();
		res.setName(budgetName);
		if (mCursor != null && mCursor.getColumnCount() >= 2) {
			mCursor.moveToFirst();
			res.setType(mCursor.getString(0));
			res.setInitialAmount(mCursor.getDouble(1));
		}

		return res;
	}
	
	public int getNbBudgets(){
    	Log.d(LOG_TAG, "getNbBudget");
		String sqlQuery = "SELECT COUNT(1) FROM "+BUDGET_TABLE+";";        

		Cursor mCursor = db.rawQuery(sqlQuery, null);

		int res = 0;
		if (mCursor != null) {
			mCursor.moveToFirst();
			res = mCursor.getInt(0);
		}

		return res;
	}
	
	public void insertExpense(Expense expense){
    	Log.d(LOG_TAG, "InsertDB expense : "+expense);

    	long epoch = System.currentTimeMillis()/1000;

		db.execSQL("INSERT INTO "+EXPENSE_TABLE+" VALUES ( null, '"+expense.getBudgetName()+"', datetime("+epoch+", 'unixepoch', 'localtime'), '"+expense.getPayment()+"', '"+expense.getComment()+"', '"+expense.getCategory()+"', '"+expense.getAmount()+"');");

    }

	public int getNbExpenses(){
    	Log.d(LOG_TAG, "getNbBudget");
		String sqlQuery = "SELECT COUNT(1) FROM "+EXPENSE_TABLE+";";        

		Cursor mCursor = db.rawQuery(sqlQuery, null);

		int res = 0;
		if (mCursor != null) {
			mCursor.moveToFirst();
			res = mCursor.getInt(0);
		}

		return res;
	}
	
	public double getTotalExpenses(String budgetName){
    	Log.d(LOG_TAG, "getTotalExpenses for budget : "+budgetName);
		String sqlQuery = "SELECT SUM("+COL_BUDGET_AMOUNT+") FROM "+EXPENSE_TABLE+" WHERE "+COL_EXPENSE_BUDGET_ID+" = '"+budgetName+"';";        

		Cursor mCursor = db.rawQuery(sqlQuery, null);

		double res = 0;
		if (mCursor != null) {
			mCursor.moveToFirst();
			res = mCursor.getDouble(0);
		}

		return res;
	}

}